7.2 Initializing the SMP Logical-to-Physical CPU Core Mapping Table
Within Symmetric Multiprocessing (SMP) architectures, the Linux kernel allocates a global array named __cpu_logical_map to establish a clean translation matrix linking each execution core's contiguous software logical ID to its concrete hardware physical ID. The kernel requires a linear, unbroken sequence of logical IDs starting from zero. However, physical core indexing on contemporary silicon can be highly non-contiguous, often dictated by specific bit patterns or cluster assignments within hardware multi-processor topology registers.
The baseline element __cpu_logical_map[0] caches the physical ID of the primary boot core currently driving the initial bootstrap pipeline. Consequently, the i-th index within this array registers the hardwired physical ID of the core assigned to logical logical ID i. For example, a standard layout could yield __cpu_logical_map[1] = 1 and __cpu_logical_map[2] = 2. If the physical ID of the primary boot core evaluates to N, the corresponding index array element __cpu_logical_map[N] is mapped back to hold a physical index placeholder value of 0.
The early routine responsible for constructing this architectural lookup table is structured as follows:
u32 __cpu_logical_map[NR_CPUS] = { [0 ... NR_CPUS-1] = MPIDR_INVALID };
void __init smp_setup_processor_id(void)
{
int i;
u32 mpidr = is_smp() ? read_cpuid_mpidr() & MPIDR_HWID_BITMASK : 0;
u32 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
cpu_logical_map(0) = cpu; // __cpu_logical_map[0] = cpu
for (i = 1; i < nr_cpu_ids; ++i)
cpu_logical_map(i) = i == cpu ? 0 : i;
set_my_cpu_offset(0);
pr_info("Booting Linux on physical CPU 0x%x\n", mpidr);
}
If the underlying processor context verifies as a Symmetric Multiprocessing environment, the variable mpidr captures the filtered, masked bits of the hardware Multiprocessor Affinity Register (MPIDR); otherwise, on single-core uniprocessor silicon, mpidr defaults directly to 0.
The macro call cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0) executes bitmask extractions to isolate the core affinity level-0 index, identifying the physical hardware ID of the specific processing core executing this code segment. The subsequent programmatic statements loop across the platform bounds to construct the __cpu_logical_map lookup array and initialize the localized memory base offsets dedicated to managing per-CPU thread data storage variables.